<template>
  <div id="app" :class="['app-wrapper', currentTheme]" @mouseover="cmouseovered">
    <router-view />
  </div>
</template>

<script>
import Cookies from "js-cookie";
export default {
  name: "App",
  computed: {
    currentTheme() {
      return this.isNightMode; // 动态返回当前主题的类名
    }
  },
  data() {
    return {
      isNightMode: "day",
      timeOut: 300 * 60 * 1000 // 300 minutes
    };
  },
  created() {
    // 在组件创建时添加监听器
  },
  mounted() {
    this.isNightMode = localStorage.getItem("isNightMode");
    this.countTime();
    window.addEventListener("keydown", this.cmouseovered);
    Cookies.get('isNightMode')
  },
  methods: {
    countTime() {
      let timeout = Cookies.get("UILogoutInterval") || this.timeOut;
      // 转成数字
      timeout = Number(timeout);

      // 如果是 0，就直接 return，不设置定时器
      if (timeout === 0) {
        return;
      }

      const vm = this;
      this.timer = setTimeout(function () {
        vm.logout();
      }, timeout);
    },
    cmouseovered() {
      clearTimeout(this.timer);
      this.countTime();
    },
    async logout() {
      const url = new URL(window.location.href);
      await this.$store.dispatch("user/logout");
      this.$router.push(
        `${url.protocol}//${url.host}/login?redirect=${this.$route.fullPath}`
      );
      clearInterval(this.tiemer);
      window.removeEventListener("keydown", this.clearTimer);
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/night.scss";
</style>
